package net;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;

import java.io.IOException;
import java.net.UnknownHostException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class QQWin {

	protected Shell shell;
	private Text text;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			QQWin window = new QQWin();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(681, 602);
		shell.setText("SWT Application");
		
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setBounds(10, 10, 643, 392);
		composite.setLayout(new RowLayout(SWT.VERTICAL));
		
		text = new Text(shell, SWT.BORDER);
		text.setBounds(10, 420, 473, 26);
		
		Button button = new Button(shell, SWT.NONE);
		
		button.setBounds(555, 416, 98, 30);
		button.setText("发送");
		
		Button button_1 = new Button(shell, SWT.NONE);
		
		button_1.setBounds(555, 481, 98, 30);
		button_1.setText("连接");
		
		Button button_2 = new Button(shell, SWT.NONE);
		
		button_2.setBounds(385, 481, 98, 30);
		button_2.setText("创建服务器");

		//创建服务器
		button_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				qq = new QQ("张三");
				try {
					qq.createServer();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				new Thread(()->{
					//接收消息的线程
					try {
						qq.read(msg->{
							shell.getDisplay().syncExec(()->{
								Label lblNewLabel = new Label(composite, SWT.NONE);
								lblNewLabel.setLayoutData(new RowData(640, SWT.DEFAULT));
								lblNewLabel.setText(msg);
								composite.layout();
							});
						});
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}).start();
			}
		});
		//连接
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				qq = new QQ("李四");
				try {
					qq.connect();
				}  catch (IOException e1) {
					e1.printStackTrace();
				}
				new Thread(()->{
					//接收消息的线程
					try {
						qq.read(msg->{
							shell.getDisplay().syncExec(()->{
								Label lblNewLabel = new Label(composite, SWT.NONE);
								lblNewLabel.setLayoutData(new RowData(640, SWT.DEFAULT));
								lblNewLabel.setText(msg);
								composite.layout();
							});
						});
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}).start();
			}
			
		});
		//发送
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					Label lblNewLabel_1 = new Label(composite, SWT.NONE);
					lblNewLabel_1.setOrientation(SWT.RIGHT_TO_LEFT);
					lblNewLabel_1.setLayoutData(new RowData(639, SWT.DEFAULT));
					lblNewLabel_1.setText(text.getText());
					qq.write(text.getText());
					text.setText("");
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
	}
	QQ qq;
	
}
